from ast import literal_eval

from flask import Blueprint, request
from exts import db
from models import Software_information



bp = Blueprint("information", __name__, url_prefix="/information")



@bp.route("/Event/write",methods=["POST"])
def Event_write():
    if request.method == "POST":
        # 判断是哪一种传参的方法
        if request.content_type.startswith('application/json'):
            S_id = request.json.get('S_id')
            infor_orders = request.json.get('infor_orders')
            Download = request.json.get('Download')
        elif request.content_type.startswith('multipart/form-data'):
            S_id = request.form.get('S_id')
            infor_orders = request.form.get('infor_orders')
            Download = request.form.get('Download')
        else:
            S_id = request.values.get("S_id")
            infor_orders = request.values.get("infor_orders")
            Download = request.values.get("Download")
        data=Software_information(S_id=S_id,infor_orders=infor_orders,Download=Download)
        db.session.add(data)
        db.session.commit()
        return "正在写入"


@bp.route('/Look/up',methods=["GET"])
def LookUp():
    id = request.values.get("id")
    # 直接查找数据发送过去
    if id is None:
        return {"code": 404, "errno": "请携带参数"}
    else:
        info_data = Software_information.query.filter_by(S_id=id).first()

        if info_data is None:
            return {"code": 404, "errno": "未查询到结果"}
        else:
            # 处理一下数据
            # "https://www.hybase.com"
            orders = info_data.infor_orders.replace("https://www.hybase.com"," ")
            return {
                "code":200,"data":{
                    "id":info_data.info_id,
                    "orders":literal_eval(orders),
                    "Sid":info_data.S_id,
                    "Download":literal_eval(info_data.Download)
                }
            }